Double_t poissonf(Double_t*x, Double_t*par) {
	return par[0] * TMath::Poisson(x[0], par[1]);
}

TH1I* fitPoisson(string filePath) {
	TH1I* histo = new TH1I("histo0", "Npe;Num of photoelectrons;Count", 200, -.5, 199.5);

	ifstream ifile(filePath.c_str());

	int buffSize = 128;
	char* buffer = new char[buffSize];
	ifile.getline(buffer, buffSize);

	string temps, xs, cs;

	for (int i = 0; !ifile.eof(); i++) {
		temps = buffer;
		size_t pos = temps.find('\t', 0);

		xs = temps.substr(0, pos);
		cs = temps.substr(pos + 1, temps.size() - (pos + 1));

//		cout << xs << "\t" << cs << endl;

		histo->Fill(atoi(xs.c_str()), atoi(cs.c_str()));

		ifile.getline(buffer, buffSize);
	}

	ifile.close();
	delete[] buffer;

	TF1 pois("pois", poissonf, 0, 30, 2);

	pois.SetParName(0, "Const");
	pois.SetParName(1, "Npe");

	pois.SetParameter(0, 1);
	pois.SetParameter(1, 1);

	gStyle->SetOptFit(111);

	histo->Fit("pois");
	return histo;
}
